#include "esUtil.h"
#include "Log.h"
#include "src/GLRender.h"
#include <string>
#include <android/asset_manager.h>

using namespace std;

string loadText(ESContext *esContext, string name)
{
	AAsset* asset = AAssetManager_open((AAssetManager*)esContext->platformData, name.c_str(), AASSET_MODE_UNKNOWN);
	off_t bufferSize = AAsset_getLength(asset);
	char* buffer=(char*)malloc(bufferSize+1);
	buffer[bufferSize]=0;
	int num = AAsset_read(asset, buffer, bufferSize);
	string result(buffer);
	AAsset_close(asset);
	
	return result;
}

GLRender *main;

extern "C" {
	bool init(ESContext *esContext) {
		main = new GLRender(esContext);
		return main->onCreate();
	}
	
	void draw(ESContext *esContext) {	
		main->onDraw();
	}
	
	void shutDown(ESContext *escontext) {
		main->onDestory();
	}
	
	int esMain (ESContext *esContext) {
			esCreateWindow(esContext, "NGLT", 320, 240, ES_WINDOW_RGB);
			if (!init(esContext))
			{
				return GL_FALSE;
			}
			esRegisterDrawFunc (esContext, draw);
			esRegisterShutdownFunc (esContext, shutDown);
			return GL_TRUE;
		}
}
